package com.wyg.blog.controller.manage_blog;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyg.blog.model.blog.Message;
import com.wyg.blog.service.blog.MessageService;
import com.wyg.blog.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: WYG
 * @time: 2020/7/6 17:58
 */
@RestController
@RequestMapping("/api/message")
public class MessageController {
    @Autowired
    MessageService messageService;

    @GetMapping("/getSomeMessages")
    @CrossOrigin
    public JsonResult<IPage<Message>> getSomeMessages(@RequestParam("current") Integer current, @RequestParam("size") Integer size){
        Page<Message> page = new Page<>(current,size);
        IPage<Message> messageIPage = messageService.selectSomeMessages(page);
        return new JsonResult<>(messageIPage,"200","留言分页查询成功!");
    }

    @GetMapping("/changeMessageState")
    @CrossOrigin
    public JsonResult<Integer> changeMessageState(Integer id){
        boolean state = messageService.selectState(id);
        boolean res = messageService.renewMessageState(id, !state);
        if(res){
            return new JsonResult<>(1,"200","状态修改成功!");
        }else {
            return new JsonResult<>(0,"1102","状态修改失败!");
        }
    }

    @GetMapping("/deleteMessage")
    @CrossOrigin
    public JsonResult<Integer> deleteMessage(Integer id){
        boolean res = messageService.removeMessage(id);
        if(res) {
            return new JsonResult<>(1,"200","删除成功!");
        } else {
            return new JsonResult<>(0,"1101","删除失败!");
        }
    }
}
